/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Validations;

import com.ControlEscolar.Beans.Enrollment;
import com.ControlEscolar.Beans.Student;
import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.EnrollmentDAO;
import com.ControlEscolar.DAOs.StudentDAO;
import com.ControlEscolar.Exceptions.EnrollmentExp;
import com.ControlEscolar.Services.ReticleService;
import java.util.ArrayList;


/**
 *
 * @author Rodna Marrufo
 */
public class ValidationEnrollment extends BasicValidation{
    ReticleService reticleService=new ReticleService();
    StudentDAO studentDAO=new StudentDAO();
    EnrollmentDAO enrollmentDAO=new EnrollmentDAO();
    ValidationExam validationExam=new ValidationExam();

   public boolean isEnrollingSecondSemester(int semester){
       return semester==2;
   }

   public int semesterComparator(int semesterToEnroll, String studentKey) throws DAOException{
       final int FIRST_SEMESTER=1;
       final int HIGHEST_SEMESTER=2;
       final int LOWEST_SEMESTER=-1;
       final int EQUAL_SEMESTER=0;
        Enrollment highestSemesterEnrollment=enrollmentDAO.queryHighestSemesterEnrollment(studentKey);
        int highestSemester=highestSemesterEnrollment.getSemester();
        if(semesterToEnroll==FIRST_SEMESTER){
            return FIRST_SEMESTER;
        }
        if(highestSemester<semesterToEnroll){
            return HIGHEST_SEMESTER;
        }
        if(highestSemester>semesterToEnroll){
            return LOWEST_SEMESTER;
        }
        return EQUAL_SEMESTER;
   }

    public boolean isEnrolledSemester(String studentKey, int semester, int ID_Curriculum) throws DAOException{
        ArrayList <Enrollment>searchEnrollmentInSemester=enrollmentDAO.queryStudentEnrollmentsInSemester(studentKey, semester, ID_Curriculum);
        return isFound(searchEnrollmentInSemester);
    }

    private int numSubjectsOfSemester(int ID_Curriculum, int semester) throws DAOException{
        ArrayList <String> subjects=reticleService.getSubjectsToStudent(ID_Curriculum, semester);
        return subjects.size();
    }

    public boolean isEnrolledTwiceSemester(String studentKey, int semester, int ID_Curriculum) throws DAOException{
       ArrayList <Enrollment>searchEnrollmentInSemester=enrollmentDAO.queryStudentEnrollmentsInSemester(studentKey, semester, ID_Curriculum);
       int numEnrollmentInSemester=searchEnrollmentInSemester.size();
       return numEnrollmentInSemester>numSubjectsOfSemester(ID_Curriculum,semester);
   }


   public boolean isFailedReenrollmentSemester(String studentKey, int semester, int ID_Curriculum) throws DAOException{
       if(isEnrolledTwiceSemester(studentKey, semester, ID_Curriculum)){
           if(isFailedSubjects(studentKey, ID_Curriculum, semester ))
             return true;
        }
        return false;
    }


   public boolean isFailedSubjects(String studentKey,int ID_Curriculum, int semester) throws DAOException{
       ArrayList<String> subjectsKeys=reticleService.getSubjectsToStudent(ID_Curriculum, semester);
       for(int index=0; index<subjectsKeys.size();index++){
             if(validationExam.examWithoutGradeOrFailed(subjectsKeys.get(index),studentKey))
                 return true;
       }
        return false;
   }

   
   public boolean isEnrolledToSubject(String subjectKey) throws DAOException{
       Enrollment searchEnrollment = enrollmentDAO.querySubjectsEnrolled(subjectKey);
       return isFound(searchEnrollment);
   }
  
    public boolean isDocumentsComplete(String studentKey) throws DAOException{
        Student searchStudentDocuments = studentDAO.queryDocuments(studentKey);
        return isFound(searchStudentDocuments);
    }

    public boolean passedMinimunOfSubjects(String studentKey, int ID_Curriculum, int semester) throws DAOException{
       final int MAX_FAILED_SUBJECTS = 3;
       if(numberFailedSubjects(ID_Curriculum, semester,studentKey)>=MAX_FAILED_SUBJECTS){
            return false;
       }
        else
            return true;
    }
    
    private int numberFailedSubjects(int ID_Curriculum, int semester, String studentKey) throws DAOException{
        ArrayList<String> subjectsKeys=reticleService.getSubjectsToStudent(ID_Curriculum, semester);
         int totalFailedSubjects=0;
          for(int index=0;index<subjectsKeys.size();index++){
              if(validationExam.examWithoutGradeOrFailed(subjectsKeys.get(index), studentKey))
                  totalFailedSubjects=totalFailedSubjects+1;
          }
         return totalFailedSubjects;
    }
  
    public boolean isStudentEnrolled(String studentKey, int ID_Curriculum) throws DAOException{
     ArrayList <Enrollment> searchStudentEnrolment=enrollmentDAO.queryActivatedErolmentsOfStudent(studentKey, ID_Curriculum);
     return isFound(searchStudentEnrolment);
    }

    public boolean isStudentFirstEnroll(String studentKey, int ID_Curriculum) throws DAOException{
        if(isStudentRevalidation(studentKey,ID_Curriculum)||isStudentEnrolled(studentKey,ID_Curriculum))
            return false;
        else
            return true;
    }

   public boolean isStudentRevalidation(String studentKey,int ID_Curriculum) throws DAOException{
      Student searchStudent=studentDAO.queryActivatedRevalidation(studentKey);
      return isFound(searchStudent);
        
   }

   public boolean isEnrollRightSubjects(ArrayList<String> subjectsToEnroll, int ID_Curriculum, int semester) throws DAOException{
       ArrayList<String> subjectsList=reticleService.getSubjectsToStudent(ID_Curriculum, semester);
       int numSubjectsOfSemester=subjectsList.size();
       int numSubjectsToEnroll=subjectsToEnroll.size();

       if(numSubjectsOfSemester!=numSubjectsToEnroll)
           return false;
       else
           return !isRepeatedSubject(subjectsToEnroll);
   }

   private boolean isRepeatedSubject(ArrayList<String> subjectsToEnroll){
       for(int index=0;index<subjectsToEnroll.size()-1;index++){
           for(int index2=index+1; index2<subjectsToEnroll.size();index2++){
               String subjectToEnroll1=subjectsToEnroll.get(index);
               String subjectToEnroll2=subjectsToEnroll.get(index2);
               if(subjectToEnroll1.compareTo(subjectToEnroll2)==0)
                   return true;
           }
       }
       return false;
   }

}
